package midoo.server.core;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

/**
 * Created by ajianzheng on 2015/4/10.
 */
public class MChannelFlowStatHandler extends ChannelDuplexHandler {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        System.err.println("KSimpleChannelInboundHandler-channelRead");
        if(msg instanceof ByteBuf) {
            ByteBuf byteBuf = (ByteBuf)msg;

            MChannelStatistics.getInstance().newBytesRead(byteBuf.readableBytes());
        }
        ///
        ctx.fireChannelRead(msg);
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if(msg instanceof ByteBuf) {
            ByteBuf byteBuf = (ByteBuf)msg;
            MChannelStatistics.getInstance().newBytesWritten(byteBuf.readableBytes());
        }

        ///
        ctx.write(msg, promise);
    }
}
